package com.loan.market.view

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.core.content.withStyledAttributes
import com.loan.market.R
import com.loan.market.databinding.LayoutTitleBinding
import com.loan.market.util.sp2px

class TitleBar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : FrameLayout(context, attrs) {

    private var layoutTitleBinding: LayoutTitleBinding

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_title, this, false)
        layoutTitleBinding = LayoutTitleBinding.bind(view)
        addView(view)
        context.withStyledAttributes(attrs, R.styleable.TitleBar) {
            layoutTitleBinding.tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, getDimension(R.styleable.TitleBar_titleSize, 16f.sp2px()))
            layoutTitleBinding.tvTitle.text = getString(R.styleable.TitleBar_title)
            layoutTitleBinding.tvTitle.setTextColor(getColor(R.styleable.TitleBar_titleColor, Color.WHITE))
            val style = getInteger(R.styleable.TitleBar_titleStyle, 0)
            if (style == 1) {
                layoutTitleBinding.tvTitle.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
            }
        }
    }

    fun setTitle(title: String?) {
        layoutTitleBinding.tvTitle.text = title
    }

    fun setOnLeftClick(onClickListener: OnClickListener) {
        layoutTitleBinding.ivBack.setOnClickListener(onClickListener)
    }

}